<?php
declare (strict_types=1);

namespace app\admin\controller;

use addons\Builder\facade\Builder;
use app\common\model\Area;
use think\exception\ValidateException;
use think\facade\View;

class Order extends Base
{

    //主键，默认为id
    protected $pk = 'id';

    //主模型
    protected $model = '\app\common\model\Order';

    //关联模型
    protected $hasOne = ['user', 'express', 'p', 'c', 'd', 'productsField'];

    protected $append = ['statusText'];

    public function index()
    {
        return Builder::Table()
            ->addColumn(['type' => 'checkbox', 'fixed' => 'left', 'title' => '全选'])
            ->addColumn(['field' => 'sn', 'title' => '订单号', 'minWidth' => 200, 'templet' => '#snTpl'])
            ->addColumn(['field' => 'code', 'title' => '下单用户', 'templet' => '#_userTpl', 'minWidth' => 200])
            ->addColumn(['field' => 'product', 'title' => '订单商品', 'width' => 300, 'templet' => '#productTpl'])
            ->addColumn(['field' => 'real_amount', 'title' => '实际支付', 'width' => 150])
            ->addColumn(['field' => 'pay_time', 'title' => '支付时间', 'width' => 150])
            ->addColumn(['field' => 'pay_way', 'title' => '支付类型', 'width' => 120])
            ->addColumn(['field' => 'statusText', 'title' => '订单状态', 'width' => 150])
            ->addColumn(['fixed' => 'right', 'align' => 'center', 'toolbar' => '#orderDemo', 'title' => '操作', 'width' => 170])
            ->addTopButtons(['deleteAll', 'export'])
            ->addSearch([
                ['uid', '用户昵称/手机号', 'input', '', '\app\common\model\User', 'code|nickname|mobile,id'],
                ['id', '商品名称', 'input,Like', '', '\app\common\model\OrderProduct', 'title,order_id'],
                ['sn', '订单编号', 'input'],
                ['status', '订单状态', 'select', $this->model::getOrderStatus('', 2)]
            ])
            ->addLineStyle('min-height: 65px;')
            ->setTableSkin('line')
            ->addRightButtons([
                'edit'    => [
                    'title'       => '详情',
                    'unity_title' => '订单详情',
                    'size'        => '1000px'
                ],
                'deliver' => [
                    'title' => '发货',
                    'url'   => url('Order/deliver')->build(),
                ],
                'delete'
            ])
            ->addExtraHtml(['order'])
            ->fetch();
    }

    public function edit($id = '', $tpl = '', $info = '')
    {
        return parent::edit($id, 'order/edit', $info); // TODO: Change the autogenerated stub
    }

    public function deliver($id = '')
    {

        if ($this->request->isPost()) {
            $param = $this->request->param();
            try {
                $this->validate($param, 'OrderDeliver');
            } catch (ValidateException $e) {
                return json(['code' => -1, 'msg' => $e->getError()]);
            }
            return json($this->model::deliver($param, $this->admin));
        }

        $info = $this->model::find($id);

        View::assign([
            'express' => \app\common\model\Express::where(['status' => 1])->select(),
            'info'    => $info
        ]);
        return View::fetch();
    }

    public function getArea($id, $level = 0)
    {
        return Area::getArea($id ?: 0, $level);
    }

}
